package cn.mldn.amr.action.abs;

import java.text.SimpleDateFormat;
import java.util.Locale;

import javax.annotation.Resource;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.ModelAndView;

public abstract class AbstractAction {
	@Resource
	private MessageSource msgSource; //自动匹配注入, 资源操作必备
	
	@InitBinder
	public void initBinder(WebDataBinder binder){ // 转换器
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		// 将自定义的转换器进行配置,表示以后如果发现有Date类型,就使用sdf对象进行转换,并且允许数据为null
		binder.registerCustomEditor(java.util.Date.class,new CustomDateEditor(sdf, true));
		
	}
	/**
	 * 设置所需要的msg与url数据,并且将数据添加到ModuleAndView中
	 */
	public void setMsgAndUrl(String msgKey, String urlKey,ModelAndView mav){
		mav.addObject("msg",this.getMsg(msgKey));
		mav.addObject("url",this.getMsg(urlKey));
		System.out.println("msg="+this.getMsg(msgKey)+":"+"url="+this.getMsg(urlKey));
	}
	
	/**
	 * 设置操作资源文件数据的读取处理
	 * @param key 资源文件的key信息
	 */
	
	public String getMsg(String key){
		try {
			return this.msgSource.getMessage(key, new Object[]{this.getFlag()}, Locale.getDefault());
			
		} catch (Exception e) {
			return null; 
		}
	}
	
	/**
	 * 传递任意多个参数
	 * @param key
	 * @param arg
	 * @return
	 */
	public String getMsg(String key, Object... arg){
		try {
			return this.msgSource.getMessage(key, arg,Locale.getDefault());
		} catch (Exception e) {
			return null;
		}
	}
	
	
	/**
	 * 返回CRUD操作时的执行标记
	 * 
	 * @return
	 */
	public abstract String getFlag();
	
	
}
